package com.note.servlet.view;

import com.note.service.view.viewService;
import com.note.service.view.viewServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

public class addComment extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        int noteId = Integer.parseInt(req.getParameter("noteId"));
        int parentId=0;
        if (!req.getParameter("parentId").equals("")){
            parentId = Integer.parseInt(req.getParameter("parentId"));
        }
        String nickname = req.getParameter("nickname");
        if(nickname==""){
            nickname = "未命名";
        }
        String content = req.getParameter("content");
        Timestamp createtime = Timestamp.valueOf(req.getParameter("createtime"));
        viewService v = new viewServiceImpl();
        boolean flag = v.addcomment(noteId, content, createtime, nickname, parentId);
        PrintWriter out = resp.getWriter();
        out.write(String.valueOf(flag));
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
